class Solution(object):
    def scoreOfParentheses(self, s):
        st = [0]
        n = len(s)
        for i in range(n):
            if s[i] == '(':
                st.append(0)
            else:
                node = st.pop()
                if not node:
                    st[-1] += 1
                else:
                    st[-1] += 2 * node
        return st[0]


data = Solution()
s = "()(((())))"
print(data.scoreOfParentheses(s))
